package com.zxkj.commonpatrol.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;

import com.czy.basecommon.utils.base.BaseVmDataBindingActivity;
import com.czy.basecommon.utils.base.StateData;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.zxkj.commonpatrol.BR;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.adapt.DeciveEventAdapt;
import com.zxkj.commonpatrol.adapt.ProjectDetailAdapt;
import com.zxkj.commonpatrol.databinding.ActivityDeviceEventDetailBinding;
import com.zxkj.commonpatrol.model.entity.DeciveEventEntity;
import com.zxkj.commonpatrol.model.entity.SelectProjectEntrity;
import com.zxkj.commonpatrol.model.response.DevEvtPatrolInfoResponse;
import com.zxkj.commonpatrol.viewmodel.DeciveEventDetailViewModel;

import java.util.List;
/**
 *desc:
 *author:mid
 *addtime:2020/7/8 16:05
 * Decive  Event  复用
 */
public class DeviceEventDetailActivity extends BaseVmDataBindingActivity<ActivityDeviceEventDetailBinding, DeciveEventDetailViewModel> {

    private int colorAccent;
    private int btn_yellow;
    private int green;
    private int white;
    private int mCurrent=0;
    private RecyclerView recyclerView;
    private DeciveEventAdapt adapt;
    private  String Fifter;
    private int Type=1;
    private int MyType=0;
    private SmartRefreshLayout smartRefreshLayout;
    private MiniLoadingDialog loadingDialog;
    private String RouteInspectNo;
    private String PointNo;
    private String DevEvtNo;
    private String QuestsNo;//问题集编号
    private String name;
    private boolean isShow=false;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_device_event_detail;
    }

    @Override
    public int initVariableId() {
        return BR.DeciveDetailViewModel;
    }

    @Override
    protected void setListener() {
        super.setListener();
        //pending
        binding.tvUpcoming.setOnClickListener(v->{

            if (mCurrent==0){
                //不處理
            }else{
                binding.clDash.setBackgroundColor(btn_yellow);
                binding.tvUpcoming.setEnabled(false);
                binding.tvUpcoming.setTextColor(btn_yellow);
                binding.tvPending.setEnabled(true);
                binding.tvPending.setTextColor(white);
                binding.tvReject.setEnabled(true);
                binding.tvReject.setTextColor(white);
                mCurrent=0;
                //刷新數據

                if (loadingDialog==null){
                    loadingDialog= new MiniLoadingDialog(this, "加載中...");
                }
                loadingDialog.show();
                viewModel.getDevPatrolInfo(RouteInspectNo,PointNo,DevEvtNo,QuestsNo,Fifter,mCurrent,MyType);
            }

        });
        //completeed
        binding.tvPending.setOnClickListener(v->{
            if (mCurrent==1){

            }else{
                binding.clDash.setBackgroundColor(green);
                binding.tvUpcoming.setEnabled(true);
                binding.tvUpcoming.setTextColor(white);
                binding.tvPending.setEnabled(false);
                binding.tvPending.setTextColor(green);
                binding.tvReject.setEnabled(true);
                binding.tvReject.setTextColor(white);
                //刷新數據
                //刷新數據
                if (loadingDialog==null){
                    loadingDialog= new MiniLoadingDialog(this, "加載中...");
                }
                loadingDialog.show();
                mCurrent=1;
                viewModel.getDevPatrolInfo(RouteInspectNo,PointNo,DevEvtNo,QuestsNo,Fifter,mCurrent,MyType);

            }

        });
        //all
        binding.tvReject.setOnClickListener(v->{
            if (mCurrent!=-1){
                binding.clDash.setBackgroundColor(colorAccent);
                binding.tvUpcoming.setEnabled(true);
                binding. tvUpcoming.setTextColor(white);
                binding. tvPending.setEnabled(true);
                binding. tvPending.setTextColor(white);
                binding. tvReject.setEnabled(false);
                binding. tvReject.setTextColor(colorAccent);
                mCurrent=-1;
                if (loadingDialog==null){
                    loadingDialog= new MiniLoadingDialog(this, "加載中...");
                }
                loadingDialog.show();
                //刷新數據
                viewModel.getDevPatrolInfo(RouteInspectNo,PointNo,DevEvtNo,QuestsNo,Fifter,mCurrent,MyType);

            }

        });
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.getDevPatrolInfo(RouteInspectNo,PointNo,DevEvtNo,QuestsNo,Fifter,mCurrent,MyType);
            }
        });
        binding.ivHeadSearch.setOnClickListener(v -> {
            Fifter=binding.edSeach.getTextEx();
            binding.multipleProjectListView.showLoading();
            viewModel.getDevPatrolInfo(RouteInspectNo,PointNo,DevEvtNo,QuestsNo,Fifter,mCurrent,MyType);

        });
          binding.multipleProjectListView.setOnRetryClickListener(v->{

              binding.multipleProjectListView.showLoading();
              viewModel.getDevPatrolInfo(RouteInspectNo,PointNo,DevEvtNo,QuestsNo,Fifter,mCurrent,MyType);
          } );
        binding.qkBack.setOnClickListener(v->{
            ActivityTool.getInstance().popOneActivity(this);
        });
        binding.edSeach.setOnFocusChangeListener((v,h)->{
            if (h){
                binding.ivHeadSearch.setImageResource(R.drawable.search_biss);
            }else{
                binding.ivHeadSearch.setImageResource(R.drawable.search);
            }
        });
        binding.btnSearch.setOnClickListener(v->{
            if (!isShow){
                binding.rlUpcoming.setVisibility(View.VISIBLE);
                isShow=true;
            }else{
                binding.rlUpcoming.setVisibility(View.GONE);
                isShow=false;
                Fifter="";
                binding.edSeach.setTextEx("");
            }
        });
    }

    @Override
    protected void dataObserver() {
        super.dataObserver();
        viewModel.Datas.observe(this, new Observer<StateData<List<DevEvtPatrolInfoResponse.DevEvtPatrolQuest>>>() {
            @Override
            public void onChanged(StateData<List<DevEvtPatrolInfoResponse.DevEvtPatrolQuest>> listStateData) {
                smartRefreshLayout.finishRefresh();
                if (loadingDialog!=null){
                    loadingDialog.dismiss();
                }
                if (listStateData.getStatus()== StateData.DataStatus.SUCCESS){
                    if (listStateData.getData().size()>0){
                       binding.multipleProjectListView.showContent();
                        if (adapt==null){
                            adapt =new DeciveEventAdapt(R.layout.item_device_event,listStateData.getData(),recyclerView,RouteInspectNo,PointNo,DevEvtNo,QuestsNo,Type);
                            recyclerView.setAdapter(adapt);

                        }else{
                            adapt.setNewData(listStateData.getData());
                            recyclerView.setAdapter(adapt);
                        }

                    }else{
                        binding.multipleProjectListView.showEmpty();
                    }
                }else{
                    binding.multipleProjectListView.showError();
                }
            }
        });
        LiveEventBus.get("EventDeciveList",String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                initDatas();
            }
        });

    }

    @Override
    protected void initUI() {
        if (getIntent().hasExtra("Type")){
            Type= getIntent().getIntExtra("Type",1);
        }
        if (Type==1){
            MyType=0;
        }else{
            MyType=1;
        }
        if (getIntent().hasExtra("RouteInspectNo")){
            RouteInspectNo= getIntent().getStringExtra("RouteInspectNo");
        }
        if (getIntent().hasExtra("PointNo")){
            PointNo= getIntent().getStringExtra("PointNo");
        }
        if (getIntent().hasExtra("DevEvtNo")){
            DevEvtNo= getIntent().getStringExtra("DevEvtNo");
        }
        if (getIntent().hasExtra("QuestsNo")){
            QuestsNo= getIntent().getStringExtra("QuestsNo");
        }
        if (getIntent().hasExtra("name")){
            name= getIntent().getStringExtra("name");
        }


            //设备
            binding.tvTitle.setText(name);

        white= getResources().getColor(R.color.white);
        colorAccent = getResources().getColor(R.color.colorAccent);
        btn_yellow = getResources().getColor(R.color.btn_yellow);
        green = getResources().getColor(R.color.green);
        LinearLayoutManager layoutManager =new LinearLayoutManager(this);
        recyclerView = (RecyclerView) findViewById(R.id.recy_project_check_pointlist_project);
        recyclerView.setLayoutManager(layoutManager);
         smartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout_recy_project_check_point_list);
        initDatas();
    }

    @Override
    protected void initData() {

    }




    protected void initDatas() {
        if (loadingDialog==null){
            loadingDialog= new MiniLoadingDialog(this, "加載中...");
        }
        loadingDialog.show();
        viewModel.getDevPatrolInfo(RouteInspectNo,PointNo,DevEvtNo,QuestsNo,Fifter,mCurrent,MyType);
    }
}
